/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nonmr;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import overfeatfeatureextraction.AvroImage;
import overfeatfeatureextraction.ImageFileFilter;

/**
 *
 * @author mwh
 */
public class ReadAvroHBase {

    public static void main(String[] args) throws Exception {

        String imagePath = "/home/mwh/tools/ml/overfeat/samples-small";

        HBasePutter hbasePutter = new HBasePutter();
        
        File imagePathFile = new File(imagePath);
        File[] imageFiles = imagePathFile.listFiles(new ImageFileFilter());
        for (File imageFile : imageFiles) {
            BufferedImage image = ImageIO.read(imageFile);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);
            baos.close();
            byte[] bytes = baos.toByteArray();

            HashFunction hf = Hashing.md5();
            HashCode hc = hf.newHasher().putBytes(bytes).hash();
            byte[] hashBytes = hc.asBytes();
            
            AvroImage avroImage = AvroImage.newBuilder().
                    setMd5(ByteBuffer.wrap(hashBytes)).
                    setContent(ByteBuffer.wrap(bytes)).
                    build();
            hbasePutter.put(avroImage);
        }
    }
    
    
}
